package urlconfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 01395265
 * @date 2020/7/27
 */
public class TestCharacter {

    static Connection connection = getConnection();

    public static void main(String[] args) throws Exception {

//        String a = "";
//
//        Charset charset = Charset.defaultCharset();
//        System.out.println(charset);
//
//        modifySessionStatus();
//        queryVariable("show session variables like '%character%';");
//        insert();
        queryData("select * from encoding;");
    }

    public static void insert() throws Exception {
        String sql = "insert into encoding(str) values (?)";
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setString(1, "娃哈哈");
        int i = statement.executeUpdate();
        System.out.println(i);
    }

    public static void modifySessionStatus() throws Exception {
        PreparedStatement namesPs = connection.prepareStatement("set session character_set_results = 'GBK';");
        namesPs.executeUpdate();
    }

    public static void queryVariable(String sql) throws Exception {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String nameCi = (String) resultSet.getObject("Variable_name");
            String nameCS = (String) resultSet.getObject("Value");
            System.out.println(nameCi + ", " + nameCS);
        }
    }

    public static void queryData(String sql) throws Exception {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Integer nameCi = (Integer) resultSet.getObject("id");
            String nameCS = (String) resultSet.getObject("str");
            System.out.println(nameCi + ", " + nameCS);
        }
    }

    private static Connection getConnection() {
        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return DriverManager.getConnection("jdbc:mysql://10.88.26.129:3306/auth?useUnicode=true&characterEncoding=UTF-8&characterSetResults=GBK", "authopr", "authopr#1q2s");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

}
